<template>
  <div class="emoji">
    <ul class="emoji-controller">
      <li 
        v-for="(pannel,index) in pannels" 
         @click="changeActive(index,pannel)"
         :key='index'
        :class="{'active': index === activeIndex}
        ">{{ pannel }}</li>
    </ul>
    <ul class="emoji-container">
      <li 
        v-for="(emojiGroup, index) in emojis" 
        style="padding: 0" 
        :key="index"
        v-if="index === activeIndex">
        <a 
          href="javascript:;" 
          v-for="(emoji, index) in emojiGroup"  
          :key="index" @click="selectItem(emoji)">
           <span class="emoji-item" :title="emoji">
             <img :src="getPureName(emoji)" width="24px"/>
           </span>
        </a>
      </li>
    </ul>
  </div>
</template>
<script>
import data from "../data/emoji-data.js";

export default {
  name: "emoji",
  data() {
    return {
      emojiData: data,
      pannels: ["表情"],
      activeIndex: 0,
      activePannel: "表情"
    };
  },
  props: ["toUser"],
  methods: {
    changeActive(index, pannel) {
      this.activeIndex = index;
      this.activePannel = pannel;
    },
    getPureName(name) {
      if (this.activePannel === "weibo") {
        let value = this.emojiData[this.activePannel][name];
        //let path = "./static/emoji/";
        return value;
      } else {
        let value = this.emojiData[this.activePannel][name];
        let path = "./static/emoji/";
        return path + value;
      }
    },
    selectItem(emoji) {
      this.$emit("select", emoji);
    }
  },
  mounted() {
    if (this.toUser.greetingType) {
      if (this.toUser.greetingType === "weibo") {
        this.pannels = [];
        this.pannels.push("weibo");
        this.activePannel = "weibo";
      }
    }
  },
  computed: {
    emojis() {
      return this.pannels.map(item => {
        return Object.keys(this.emojiData[item]);
      });
    }
  }
};
</script>

<style lang='scss' scoped>
@import "../assets/scss/emoji-sprite.scss";

.emoji {
  width: 380px;
  height: 186px;
  bottom: 30px;
  background: #fff;
  z-index: 10;
  padding: 10px;
  margin-right: 10px;
  .emoji-controller {
    height: 36px;
    overflow: hidden;
    margin-bottom: 0;
    li {
      float: left;
      width: 76px;
      font-size: 12px;
      line-height: 36px;
      cursor: pointer;
      text-align: center;
      position: relative;
      &.active::after {
        content: "";
        width: 100%;
        height: 1px;
        background: #62bad9;
        left: 0;
        bottom: 4px;
        position: absolute;
      }
    }
  }
  .emoji-container {
    height: 140px;
    overflow-y: auto;
    overflow-x: hidden;
    position: relative;
    li {
      font-size: 0;
      padding: 5px;
      a {
        float: left;
        overflow: hidden;
        width: 10%;
        height: 35px;
        transition: all ease-out 0.2s;
        border-radius: 4px;

        span {
          width: 25px;
          height: 25px;
          display: inline-block;
          border: 1px solid transparent;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
